package cn.com.haopy.yue.ai.wx.web;


import cn.com.haopy.yue.ai.wx.service.WxPayCommService;
import cn.com.haopy.yue.ai.wx.web.base.BaseWxController;
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.bean.result.BaseWxPayResult;
import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RequestMapping("/haopyYueAi/wxPay")
@Api(tags = "微信支付回调接口")
public class WxPayBackController extends BaseWxController {

    private final Log logger = LogFactory.getLog(WxPayBackController.class);

    @Autowired
    @Qualifier("wxMaPayService")
    private WxPayService wxMaPayService;


    @Autowired
    WxPayCommService wxPayCommService;


    /**
     * @return
     */
    @PostMapping("notify")
    @ApiOperation("微信支付回调")
    public Object payNotify() {
        String xmlResult = null;
        try {
            xmlResult = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding());
        } catch (IOException e) {
            e.printStackTrace();
            return WxPayNotifyResponse.fail(e.getMessage());
        }

        WxPayOrderNotifyResult result = null;
        try {
            result = wxMaPayService.parseOrderNotifyResult(xmlResult);

            if (!WxPayConstants.ResultCode.SUCCESS.equals(result.getResultCode())) {
                logger.error(xmlResult);
                throw new WxPayException("微信通知支付失败！");
            }
            if (!WxPayConstants.ResultCode.SUCCESS.equals(result.getReturnCode())) {
                logger.error(xmlResult);
                throw new WxPayException("微信通知支付失败！");
            }
            logger.info("处理腾讯支付平台的订单支付");
            logger.info(result);

            String orderSn = result.getOutTradeNo();
            String payId = result.getTransactionId();

            // 分转化成元
            String totalFee = BaseWxPayResult.fenToYuan(result.getTotalFee());

            return payWxNotify(orderSn, payId);

        } catch (WxPayException e) {
            e.printStackTrace();
            return WxPayNotifyResponse.fail(e.getMessage());
        }


    }

    public Object payWxNotify(String payNo, String payId) {
        Boolean bool = wxPayCommService.wxPayBack(payNo, payId);
        if (bool) {
            return WxPayNotifyResponse.success("支付回到处理成功!");
        }
        return WxPayNotifyResponse.fail("支付回到处理失败");


    }


}
